﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Management;

namespace NetC
{
    class HandleReg
    {

        public static void set_regValue(string name, string value)
        {
            RegistryKey mysoftware = Registry.CurrentUser.OpenSubKey("software", true);
            RegistryKey mytimediff = mysoftware.OpenSubKey("NetC", true);
            mytimediff.SetValue(name, value);
            mytimediff.Close();
            mysoftware.Close();
        }

        public static string get_regValue(string name)
        {
            RegistryKey mysoftware = Registry.CurrentUser.OpenSubKey("software", true);
            RegistryKey subkey = mysoftware.OpenSubKey("NetC", true);
            if (subkey != null)
            {
                string str = subkey.GetValue(name).ToString();
                mysoftware.Close();
                return str;
            }
            return null;
        }

        public static void resetReg()
        {

            RegistryKey mysoftware = Registry.CurrentUser.OpenSubKey("software", true);
            RegistryKey netconn = mysoftware.CreateSubKey("NetC");
            if (netconn == null)
                netconn = mysoftware.CreateSubKey("NetC");

            netconn.SetValue("username", "");
            netconn.SetValue("password", "");
            netconn.SetValue("UID", "");
            netconn.SetValue("username", "");
            netconn.SetValue("password", "");
            netconn.SetValue("mac", GetNetCardMacAddress());
            netconn.SetValue("timeDiff", "0");
            netconn.SetValue("model", "1"); //default nation
            netconn.SetValue("ctype", "2"); //default customer
            //client parameters
            netconn.SetValue("startrun", "0");
            netconn.SetValue("cserver", "10.0.0.55");
            netconn.SetValue("cport", "3333");
            netconn.SetValue("cversion", "14");
            //web parameters
            netconn.SetValue("wserver", "10.0.0.55");
            netconn.SetValue("wversion", "100");

            netconn.SetValue("exception", "0");
            netconn.SetValue("limitTh", "100");
            //change mode time latency
            netconn.SetValue("cmlatency", "60");
            //reminder
            netconn.SetValue("moneyrm", "1");
            //srun msg
            netconn.SetValue("srunmsg", "0");
            netconn.Close();
            mysoftware.Close();
        }

        public static string GetNetCardMacAddress()
        {
            ManagementClass mc;
            ManagementObjectCollection moc;
            mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                {
                    return mo["MacAddress"].ToString();
                }

            }
            return "";
        }

        public static string getRegValue(string regName)
        {
            RegistryKey mysoftware = Registry.CurrentUser.OpenSubKey("software", true);
            RegistryKey netconn = mysoftware.CreateSubKey("NetC");
            if (netconn == null)
            {
                mysoftware.Close();
                netconn.Close();
                return null;
            }
            else
            {
                string str = netconn.GetValue(regName).ToString();
                mysoftware.Close();
                netconn.Close();
                return str;
            }
        }


        //simple encryption
        public static string passencode_in(string pw)
        {
            string pw2 = "";
            char temp;
            for (int i = 0; i < pw.Length; i++)
            {
                temp = (char)(pw[i] ^ 77);
                pw2 += temp;
            }
            return pw2;
        }
    }

    struct rm_info
    {
        public bool rm;
        public string[] rm_str;
    }

    class reminder
    {
          //check the remain money
        public static rm_info money_reminder(ref string Info)
        {
            rm_info myinfo;
            myinfo.rm = false;
            myinfo.rm_str = new string[2];
            if (HandleReg.get_regValue("moneyrm") == "1")
            {
                int[] n_leapy = { 365, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };    //leap year
                int[] leapy = { 366, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };   //not leap year
                int year = DateTime.Now.Year, month = DateTime.Now.Month, day = DateTime.Now.Day, check_day; ;
                if ((year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))    //  leapyear
                {
                    check_day = leapy[month] - 2;
                }
                else
                {
                    check_day = n_leapy[month] - 2;
                }
                if (check_day <= day)
                {
                    myinfo.rm = true;
                    string check_str = Info.Substring(Info.LastIndexOf("帐户余额"));
                    check_str = check_str.Substring(check_str.IndexOf("<p>") + 3, check_str.IndexOf("元") - check_str.IndexOf("<p>") - 3);
                    double yu_e = Convert.ToDouble(check_str);
                    myinfo.rm_str[0] = "NetC余额结算提醒";
                    if (yu_e < 25.00)
                    {
                        myinfo.rm_str[1] = "您的当前帐户余额为" + check_str + "元（余额不足）" +
                                  "\n离下一结算时间仅有" + (leapy[month] - day).ToString() + "天" +
                                  "\n请您及时充值!";
                    }
                    else
                    {
                        myinfo.rm_str[1] = "您的当前帐户余额为" + check_str + "元" +
                                   "\n离下一结算时间还有" + (leapy[month] - day).ToString() + "天" +
                                   "\n请您根据使用需要进行充值!";
                    }
                    return myinfo;
                }
               
            }

            return myinfo;
        }
        
    }
}
